package es.udc.emarketapp.http.view.actionForms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;

public class ChangePasswordForm extends DefaultActionForm {

    private String oldPassword;
    private String newPassword;
    private String retypePass;

    public ChangePasswordForm() {
        reset();
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getRetypePass() {
        return retypePass;
    }

    public void setRetypePass(String retypePass) {
        this.retypePass = retypePass;
    }

     public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        PropertyValidator.validateMandatory(errors, "oldPassword", oldPassword);
        boolean validatePassword = PropertyValidator.validateMandatory(errors,
            "newPassword", newPassword);
        validatePassword = validatePassword &&
            PropertyValidator.validateMandatory(errors, "retypeNewPassword",
                retypePass);

        if (validatePassword && !newPassword.equals(retypePass)) {
            errors.add("newPassword",
                new ActionMessage("ErrorMessages.newPassword.doNotMatch"));
        }

        return errors;

    }

  
    private void reset() {
        oldPassword = null;
        newPassword = null;
        retypePass = null;
    }
}
